package HZW;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import ZMJ.danci;

public class add extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTable table;
	private DefaultTableModel tableModel;
	public JTextField aTextField;
	public JTextField bTextField;
	private JScrollPane scrollPane;

	/**
	 * @param args
	 */
	// public static void main(String[] args) {
	// // TODO Auto-generated method stub
	// add frame = new add();
	// frame.setVisible(true);
	// }

	public add() {
		setTitle("词库管理");
		setBounds(600, 400, 700, 200);
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		scrollPane = new JScrollPane();
		scrollPane.setViewportView(getTable());
		getContentPane().add(scrollPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();

		// JLabel aLabel = new JLabel("词库编号:");
		// panel.add(aLabel);
		// aTextField = new JTextField(15);
		// panel.add(aTextField);

		JLabel bLabel = new JLabel("词库名称:");
		panel.add(bLabel);
		bTextField = new JTextField(15);
		panel.add(bTextField);

		JButton addButton = new JButton("添加");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = bTextField.getText();
				// String baseid = aTextField.getText();

				if (!"".equals(str)) {
					// int baseidd =Integer.parseInt(baseid);
					if (baseDao.add(str)) {
						JOptionPane.showMessageDialog(null, "添加成功！");
						table = null;
						// aTextField.setText(null);
						bTextField.setText(null);
						scrollPane.setViewportView(getTable());
					} else
						JOptionPane.showMessageDialog(null, "系统异常 ，添加失敗！");
				} else {
					JOptionPane.showMessageDialog(null, "请正确输入！");
				}
			}
		});
		panel.add(addButton);

		JButton delButton = new JButton("删除");
		delButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() < 0) {
					JOptionPane.showMessageDialog(null, "您没有选中任何行！");
				} else {
					int selectedRow = Integer.parseInt((String) table
							.getValueAt(table.getSelectedRow(), 0));
					if (baseDao.del(selectedRow)) {
						JOptionPane.showMessageDialog(null, "删除成功！");
					} else {
						JOptionPane.showMessageDialog(null, "系统异常 ，删除失败！");
					}
					table = null;
					scrollPane.setViewportView(getTable());
				}
			}
		});
		panel.add(delButton);

		JButton editButton = new JButton("修改");
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() < 0) {
					JOptionPane.showMessageDialog(null, "您没有选中任何行！");
				} else {
					// String baseid = aTextField.getText();
					String basenames = bTextField.getText();
					int selectedid = Integer.parseInt((String) table
							.getValueAt(table.getSelectedRow(), 0));
					if ("".equals(basenames)) {
						JOptionPane.showMessageDialog(null, "请正确输入词库名称！");
					} else {
						// int baseidd =Integer.parseInt(baseid);

						if (baseDao.edit(basenames, selectedid)) {
							JOptionPane.showMessageDialog(null, "修改成功！");
							table = null;
							// aTextField.setText(null);
							bTextField.setText(null);
							scrollPane.setViewportView(getTable());
						}
					}
				}
			}

		});
		panel.add(editButton);

		JButton wordButton = new JButton("添加单词");
		wordButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new danci();
			}

		});
		panel.add(wordButton);

		getContentPane().add(panel, BorderLayout.SOUTH);
		setVisible(true);
	}

	public JTable getTable() {

		if (table == null)
			;
		table = new JTable();

		String[] columnNames = { "词库编号", "词库名称" };

		java.util.List<wordbase> bases = baseDao.getBases();

		tableModel = new DefaultTableModel(columnNames, 0);// DefaultTableModel可以对表格进行数据的增删改
		table = new JTable(tableModel);

		for (wordbase wb : bases) {
			String[] temp = { String.valueOf(wb.getId()), wb.getBasename() };
			tableModel.addRow(temp);
		}

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置表格行的选择模式，这里设置为一次只能选择一项

		return table;
	}

	// TODO Auto-generated method stub

}
